//
//  VideoView.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/20.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "VideoView.h"
@interface VideoView()

@property (nonatomic, strong) UIButton *publishBtn;
@property (nonatomic, strong) UIView *recommendView;
@property (nonatomic, strong) UIView *selectTagView;
@property (nonatomic, strong) UIView *tagHideView;
@property (nonatomic, strong) NSMutableArray *buttonsArr;
@property (nonatomic, strong) UIView *tagView;
@property (nonatomic, strong) UIImageView *showRecommendImg;

@property (nonatomic, assign) BOOL isShowRecoomed;
@end

@implementation VideoView

- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = KLColor_C1;
        [self loadSubviewsOnVideoView];
    }
    return self;
}

- (void)loadSubviewsOnVideoView{
    UIView *titleView = [[UIView alloc]init];
    titleView.backgroundColor = [UIColor whiteColor];
    [self addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10*HEIGHT);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(85*HEIGHT);
    }];
    
    UILabel *tintLb = [[UILabel alloc]init];
    tintLb.text = @"标题";
    tintLb.textColor = KLColor_B6;
    tintLb.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16*WIDTH];
    [titleView addSubview:tintLb];
    [tintLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView).offset(15*HEIGHT);
        make.left.equalTo(titleView).offset(15*WIDTH);
    }];
    
    UILabel *tintSelectTint = [[UILabel alloc]init];
    tintSelectTint.text = @"（非必填）";
    tintSelectTint.textColor = KLColor_B4;
    tintSelectTint.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    [titleView addSubview:tintSelectTint];
    [tintSelectTint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView).offset(19*HEIGHT);
        make.left.equalTo(tintLb.mas_right).offset(10*WIDTH);
    }];
    
    UITextField *contentTF = [[UITextField alloc]init];
    contentTF.placeholder = @"输入视频标题，不超过30字";
    contentTF.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
    contentTF.textColor = KLColor_B6;
    [titleView addSubview:contentTF];
    [contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tintLb.mas_bottom).offset(15*WIDTH);
        make.left.equalTo(titleView).offset(15*WIDTH);
    }];
    
    UIView *tintLine = [[UIView alloc]init];
    tintLine.backgroundColor = KLColor_B3;
    [titleView addSubview:tintLine];
    [tintLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleView).offset(15*WIDTH);
        make.right.bottom.equalTo(titleView);
        make.height.mas_equalTo(1);
        
    }];
    
    // 选择分类
    UIView *selectTypeView = [[UIView alloc]init];
    selectTypeView.backgroundColor = [UIColor whiteColor];
    selectTypeView.tag = 100;
    UITapGestureRecognizer *tapRecord = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClicks:)];
    [selectTypeView addGestureRecognizer:tapRecord];
    [self addSubview:selectTypeView];
    [selectTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(52*HEIGHT);
    }];
    
    UILabel *leftTint = [[UILabel alloc]init];
    leftTint.text = @"分类";
    leftTint.textColor = KLColor_B6;
    leftTint.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16*WIDTH];;
    [selectTypeView addSubview:leftTint];
    [leftTint mas_makeConstraints:^(MASConstraintMaker *make) {
  
        make.centerY.equalTo(selectTypeView);
        make.left.equalTo(selectTypeView).offset(15*WIDTH);
    }];
    
    UILabel *selectLb = [[UILabel alloc]init];
    selectLb.text = @"选择分类";
    selectLb.textColor = KLColor_B6;
    selectLb.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    [selectTypeView addSubview:selectLb];
    self.selectLb = selectLb;
    [selectLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectTypeView);
        make.right.equalTo(selectTypeView).offset(-38*WIDTH);
    }];
    
    UIImageView *typeImageView = [[UIImageView alloc]init];
    typeImageView.image = [UIImage imageNamed:@"luzhi_icon_biaoqian_into"];
    [selectTypeView addSubview:typeImageView];
    [typeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectTypeView);
        make.right.equalTo(selectTypeView).offset(-15*WIDTH);
    }];
    
    
    UIView *typeLine = [[UIView alloc]init];
    typeLine.backgroundColor = KLColor_B3;
    [selectTypeView addSubview:typeLine];
    [typeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(selectTypeView);
        make.left.equalTo(selectTypeView).offset(15*WIDTH);
        make.height.mas_equalTo(1);
    }];
    // 选择标签
    
    UIView *selectTagView = [[UIView alloc]init];
    selectTagView.backgroundColor = [UIColor whiteColor];
    selectTagView.tag = 101;
    UITapGestureRecognizer *tapTag = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClicks:)];
    [selectTagView addGestureRecognizer:tapTag];
    [self addSubview:selectTagView];
    self.selectTagView = selectTagView;
    [selectTagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selectTypeView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(52*HEIGHT);
    }];
    
    UILabel *leftTint_tag = [[UILabel alloc]init];
    leftTint_tag.text = @"标签";
    leftTint_tag.textColor = KLColor_B6;
    leftTint_tag.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16*WIDTH];;
    [selectTagView addSubview:leftTint_tag];
    [leftTint_tag mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(selectTagView);
        make.left.equalTo(selectTagView).offset(15*WIDTH);
    }];
    
    UILabel *selectLb_tag = [[UILabel alloc]init];
    selectLb_tag.text = @"选择标签";
    selectLb_tag.textColor = KLColor_B6;
    selectLb_tag.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14*WIDTH];
    [selectTagView addSubview:selectLb_tag];
    [selectLb_tag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectTagView);
        make.right.equalTo(selectTagView).offset(-38*WIDTH);
    }];
    
    UIImageView *typeImageView_tag = [[UIImageView alloc]init];
    typeImageView_tag.image = [UIImage imageNamed:@"luzhi_icon_biaoqian_into"];
    [selectTagView addSubview:typeImageView_tag];
    [typeImageView_tag mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectTagView);
        make.right.equalTo(selectTagView).offset(-15*WIDTH);
    }];
    
    
    UIView *tagHideView = [[UIView alloc]init];
    tagHideView.backgroundColor = [UIColor whiteColor];
    tagHideView.hidden = YES;
    [self addSubview:tagHideView];
    self.tagHideView = tagHideView;
    [tagHideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selectTagView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(50*HEIGHT);
    }];
    
    
    
    // recommend video
    UIView *recommendView = [[UIView alloc]init];
    recommendView.backgroundColor = [UIColor clearColor];
    recommendView.tag = 102;
    UITapGestureRecognizer *tapRecommend = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClicks:)];
    [recommendView addGestureRecognizer:tapRecommend];
    [self addSubview:recommendView];
    self.recommendView = recommendView;
    [recommendView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selectTagView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(65*HEIGHT);
    }];
    
    UIImageView *showSelImageView = [[UIImageView alloc]init];
    showSelImageView.image = [UIImage imageNamed:@"icon_xuanzhe"];
    [self addSubview:showSelImageView];
    self.showRecommendImg = showSelImageView;
    [showSelImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(recommendView).offset(25*HEIGHT);
        make.left.equalTo(recommendView).offset(128*WIDTH);
        make.width.height.mas_equalTo(20*WIDTH);
    }];
    
    UILabel *tintSel = [[UILabel alloc]init];
    tintSel.text = @"标为推荐视频";
    tintSel.textColor = KLColor_B6;
    tintSel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:14*WIDTH];
    [recommendView addSubview:tintSel];
    [tintSel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(recommendView).offset(25*HEIGHT);
        make.left.equalTo(showSelImageView.mas_right).offset(15*WIDTH);
    }];
    
    // tint
    UILabel *alertLb = [[UILabel alloc]init];
    alertLb.text = @"不得上传淫秽色情及低俗信息，违者必究";
    alertLb.textColor = KLColor_A1;
    alertLb.font =[UIFont fontWithName:@"PingFangSC-Regular" size:12*WIDTH];
    [self addSubview:alertLb];
    [alertLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(recommendView.mas_bottom).offset(20*HEIGHT);
        make.centerX.equalTo(self);
    }];
    
    // 发布的button
    UIButton *publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [publishBtn setTitle:@"发布" forState:UIControlStateNormal];
    [publishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    publishBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16*WIDTH];
    publishBtn.backgroundColor = KLColor_A1;
    self.publishBtn = publishBtn;
    [[publishBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *publishBtn) {
        
    }];
    
    [self addSubview:publishBtn];
    [publishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(alertLb.mas_bottom).offset(51*HEIGHT);
        make.height.mas_equalTo(44*HEIGHT);
        make.width.mas_equalTo(345*WIDTH);
    }];
}

- (void)tapClicks:(UITapGestureRecognizer *)tap{
    if (tap.view.tag == 100) {
        if (self.typeSignalDelegate) {
            [self.typeSignalDelegate sendNext:nil];
        }

    }else if (tap.view.tag == 101){
        if (self.tagView) {
            [self.tagView removeAllSubviews];
            [self.tagHideView removeAllSubviews];
            [self.buttonsArr removeAllObjects];
        }
        if (self.tagSignalDelegate) {
            [self.tagSignalDelegate sendNext:nil];
        }
        
        
    }else if (tap.view.tag == 102){
        
        if (self.isShowRecoomed) {
            self.showRecommendImg.image = [UIImage imageNamed:@"icon_xuanzhe"];
        }else{
            self.showRecommendImg.image = [UIImage imageNamed:@"icon_xuanzhe_pre"];
        }
        self.isShowRecoomed = !self.isShowRecoomed;
        if (self.publishDelegate) {
            [self.publishDelegate sendNext:nil];
        }
    }
}

- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];
    [self.publishBtn layoutIfNeeded];
    [self.publishBtn setRadiusWithValue:22*HEIGHT];

}


- (void)addTagViewWithData:(NSArray *)dataArray{
    self.tagHideView.hidden = NO;
    UIView *tagView = [[UIView alloc]init];
    tagView.backgroundColor = [UIColor whiteColor];
    self.tagView = tagView;
    [self.tagHideView addSubview:tagView];
    [tagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tagHideView);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(50*HEIGHT);
    }];
    
    
    for (NSInteger i = 0; i < dataArray.count; i++) {
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [titleBtn setTitle:dataArray[i] forState:UIControlStateNormal];
        [titleBtn setTitleColor:KLColor_B7 forState:UIControlStateNormal];
        titleBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13*WIDTH];
        titleBtn.contentEdgeInsets = UIEdgeInsetsMake(5*HEIGHT, 10.5*HEIGHT, 7*HEIGHT, 10.5*HEIGHT);
        titleBtn.backgroundColor = KLColor_A9;
        titleBtn.layer.cornerRadius = 2;
        
        titleBtn.enabled = NO;
        [tagView addSubview:titleBtn];
        [titleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            NSString * str = dataArray[i];
            make.top.equalTo(tagView);
            if (i == 0) {
                make.left.equalTo(tagView).offset(15*WIDTH);
                
            }else{
                UIButton *tempBtn = self.buttonsArr[i - 1];
                make.left.equalTo(tempBtn.mas_right).offset(10*WIDTH);
            }
            
            if (str.length ==  4) {
                make.width.mas_equalTo(74*WIDTH);
            }
            if (str.length == 3) {
                make.width.mas_equalTo(60*WIDTH);
            }
            if (str.length == 2) {
                make.width.mas_equalTo(47*WIDTH);
            }
            
            
            make.height.mas_equalTo(30*HEIGHT);
            
        }];
        
        [self.buttonsArr addObject:titleBtn];
    }
    
    
    
    [self.recommendView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.selectTagView.mas_bottom).offset(50*HEIGHT);
    }];
}



+ (CGFloat)returnHeightWithTagArr:(NSArray *)dataArr{
    if (dataArr.count > 0) {
        return 463*HEIGHT;
    }
    return 413*HEIGHT;
}


- (NSMutableArray *)buttonsArr{
    if (!_buttonsArr) {
        _buttonsArr = [NSMutableArray array];
    }
    return _buttonsArr;
}



@end
